﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using jNET.Controls;

namespace jNET.Controls
{
    public class Window : Control
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Window Title")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public String Title
        {
            get;
            set;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Navigation URL")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public String URL
        {
            get;
            set;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Fire after window is closed")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public String OnClientClose
        {
            get;
            set;
        }

        public string GetOnClientCloseString()
        {
            return OnClientClose == "" ? "" : OnClientClose + "(windowManager.returnValue);";
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Window Height")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public int WindowHeight
        {
            get;
            set;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Window Width")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public int WindowWidth
        {
            get;
            set;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Allow Resizing")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool AllowResizing
        {
            get;
            set;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), Description("Allow Scrolling")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public bool AllowScrolling
        {
            get;
            set;
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            string script = "<script type='text/javascript'>\r\n" +
                "    var wnd_{0} = {{\r\n" +
                "        open: function(){{\r\n" +
                "            var resize = '0';\r\n" +
                "            if(this.allowResize == true)\r\n" +
                "                resize = '1';\r\n" +
                "            var scrolling = '0';\r\n" +
                "            if(this.allowScrolling == true)\r\n" +
                "                scrolling = '1';\r\n" +
                "            windowManager.returnValue = null;\r\n" +
                "            var wnd = dhtmlmodal.open('wnd_{0}', 'iframe', this.navigateUrl, this.title, 'width=' + this.width + 'px,height=' + this.height +'px,resize=' + resize + ',scrolling=' + scrolling + ',center=1');\r\n" +
                "            windowManager.currentWindow = wnd;\r\n" +
                "            wnd.onclose = function(){{\r\n" +
                "                {1}\r\n" +
                "            }};\r\n" +
                "        }},\r\n" +
                "        title : '{2}',\r\n" +
                "        navigateUrl : '{3}',\r\n" +
                "        returnValue : '{4}',\r\n" +
                "        width : {5},\r\n" +
                "        height : {6},\r\n" +
                "        allowResize : {7},\r\n" +
                "        allowScrolling : {8}\r\n" +
                "    }}\r\n" +
                "</script>\r\n";
            writer.Write(script, ID, GetOnClientCloseString(), Title, URL, "", WindowWidth, WindowHeight, ControlUtil.GetJsBoolString(AllowResizing), ControlUtil.GetJsBoolString(AllowScrolling));        }
    }
}